package com.bdzl.auth.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.ClientException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.bdzl.auth.common.utils.JwtUtil;
import com.bdzl.auth.service.CheckService;
import com.bdzl.auth.service.MeauService;
import com.bdzl.auth.service.UserService;
import com.bdzl.pub.entity.sys.UserDO;
import com.bdzl.pub.util.param.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
import java.util.Set;

@Slf4j
@Service
public class CheckServiceImpl implements CheckService {
	@Autowired
	private UserService userService;
	@Autowired
	private MeauService meauService;
	@Override
	public Boolean checkLogin() {
		// 获取登录用户信息，判断是否登录
		HttpServletRequest request = this.getHttpServletRequest();
		String accessToken = Objects.requireNonNull(request).getHeader("X-Access-Token");
		String username = JwtUtil.getUsername(accessToken);
		UserDO userDO = userService.getByUserLgname(username);
		if(null == userDO || userDO.getUserId() <=0) {
			return false;
		}
		return true;
	}

	@Override
	public UserDO getUserInfo(String token) {

		String username = JwtUtil.getUsername(token);
		UserDO userDO = userService.getByUserLgname(username);
		if(null == userDO || userDO.getUserId() <=0) {
			return null;
		}
		userDO.setUserPwd(null);
		return userDO;
	}


	/**
	 * 权限判断接口：先查询到资源对应的id，然后根据用户权限判断
	 *
	 */
	@Override
	public R checkPermission(HttpServletRequest request,String token, String checkUrl) {
		UserDO user = this.getUserInfo(token);
		if(null == user || user.getUserId() <=0) {
			return R.error("未登录");
		}

		// 获取用户拥有的权限集合，比如“sys:role:add,sys:user:add”
		Set<String> permissionSet = meauService.listUserPerms(user.getUserId());

		// 用户完全没有权限, 且请求资源不是开放资源
		if(null == permissionSet || permissionSet.size() <= 0) {
			log.info("当前用户未分配权限:" + user.getUserNickname());
			return R.error("无权限!");
		}
		//获取url的注解内容
		String permissionsByUrl = this.getPermissionsByUrl(checkUrl);
		if (permissionsByUrl != null && !"null".equals(permissionsByUrl)){
			// 系统配置了权限
			if(permissionSet.contains(permissionsByUrl)) {
				System.out.println(R.ok(JSONObject.toJSONString(user)));
				return R.ok(JSONObject.toJSONString(user));
			}
		}else {
			System.out.println(R.ok(JSONObject.toJSONString(user)));
			return R.ok(JSONObject.toJSONString(user));
		}
		System.out.println(R.error("没有权限！"));
		return R.error("没有权限！");
	}



	public String getPermissionsByUrl( String url) {
		String result = null;
		//获得第一个/的位置
		int index=url.indexOf("/");
		System.out.println(index);
		//根据第一个/的位置 获得第二个/的位置
		index=url.indexOf("/", index+1);
		//根据第二个点的位置，截取 字符串。得到结果 result
		result=url.substring(index);
		//输出结果
		System.out.println("------"+result);
		result=meauService.getPermsByUrl(result);
		return result;
	}
	private HttpServletRequest getHttpServletRequest() {
		try {
			ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
			HttpServletRequest request = attributes.getRequest();
			return request;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}


}
